//=====================================================================
//
// simple_test.cpp - KCP简单测试示例
//
// 编译命令：g++ simple_test.cpp ../ikcp.c -o simple_test -std=c++11
//
//=====================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>
#include <queue>

#include "../ikcp.h"

// 简单的网络模拟器
class SimpleNetwork {
public:
    struct Packet {
        int peer;
        std::vector<char> data;
        unsigned int time;
    };
    
    std::queue<Packet> packets;
    unsigned int current_time;
    
    SimpleNetwork() : current_time(0) {}
    
    void send(int peer, const char* data, int len) {
        Packet pkt;
        pkt.peer = peer;
        pkt.data.assign(data, data + len);
        pkt.time = current_time + 50; // 模拟50ms延迟
        packets.push(pkt);
    }
    
    bool recv(int peer, char* buffer, int& len) {
        if (packets.empty()) return false;
        
        Packet& pkt = packets.front();
        if (pkt.time > current_time || pkt.peer != peer) return false;
        
        len = pkt.data.size();
        memcpy(buffer, pkt.data.data(), len);
        packets.pop();
        return true;
    }
    
    void update(unsigned int time) {
        current_time = time;
    }
};

SimpleNetwork* g_network = nullptr;

// KCP输出回调函数
int udp_output(const char *buf, int len, ikcpcb *kcp, void *user) {
    int peer = *(int*)user;
    g_network->send(peer, buf, len);
    printf("[OUTPUT] peer=%d len=%d\n", peer, len);
    return 0;
}

// 获取当前时间（毫秒）
unsigned int get_time() {
    return (unsigned int)(clock() * 1000 / CLOCKS_PER_SEC);
}

int main() {
    printf("=== KCP 简单测试示例 ===\n\n");
    
    // 创建网络模拟器
    g_network = new SimpleNetwork();
    
    // 创建两个KCP端点
    int peer1 = 0, peer2 = 1;
    ikcpcb *kcp1 = ikcp_create(0x12345678, &peer1);
    ikcpcb *kcp2 = ikcp_create(0x12345678, &peer2);
    
    // 设置输出回调
    ikcp_setoutput(kcp1, udp_output);
    ikcp_setoutput(kcp2, udp_output);
    
    // 配置KCP参数
    ikcp_wndsize(kcp1, 32, 32);
    ikcp_wndsize(kcp2, 32, 32);
    ikcp_nodelay(kcp1, 1, 10, 2, 1); // 启用快速模式
    ikcp_nodelay(kcp2, 1, 10, 2, 1);
    
    printf("1. 发送数据测试\n");
    const char* message = "Hello KCP!";
    int ret = ikcp_send(kcp1, message, strlen(message));
    printf("   kcp1发送消息: \"%s\" (返回值: %d)\n", message, ret);
    
    // 模拟网络传输过程
    char buffer[1024];
    int len;
    
    for (int i = 0; i < 10; i++) {
        unsigned int current = get_time() + i * 10;
        g_network->update(current);
        
        // 更新KCP状态
        ikcp_update(kcp1, current);
        ikcp_update(kcp2, current);
        
        // 处理网络包
        while (g_network->recv(1, buffer, len)) {
            printf("   kcp2接收到网络包，长度: %d\n", len);
            ikcp_input(kcp2, buffer, len);
        }
        
        while (g_network->recv(0, buffer, len)) {
            printf("   kcp1接收到网络包，长度: %d\n", len);
            ikcp_input(kcp1, buffer, len);
        }
        
        // 尝试接收数据
        int recv_len = ikcp_recv(kcp2, buffer, sizeof(buffer));
        if (recv_len > 0) {
            buffer[recv_len] = 0;
            printf("   kcp2接收到消息: \"%s\"\n", buffer);
            break;
        }
    }
    
    printf("\n2. 双向通信测试\n");
    const char* reply = "Hello back!";
    ikcp_send(kcp2, reply, strlen(reply));
    printf("   kcp2发送回复: \"%s\"\n", reply);
    
    for (int i = 0; i < 10; i++) {
        unsigned int current = get_time() + 100 + i * 10;
        g_network->update(current);
        
        ikcp_update(kcp1, current);
        ikcp_update(kcp2, current);
        
        while (g_network->recv(0, buffer, len)) {
            ikcp_input(kcp1, buffer, len);
        }
        
        while (g_network->recv(1, buffer, len)) {
            ikcp_input(kcp2, buffer, len);
        }
        
        int recv_len = ikcp_recv(kcp1, buffer, sizeof(buffer));
        if (recv_len > 0) {
            buffer[recv_len] = 0;
            printf("   kcp1接收到回复: \"%s\"\n", buffer);
            break;
        }
    }
    
    printf("\n3. 状态信息\n");
    printf("   kcp1等待发送的包数量: %d\n", ikcp_waitsnd(kcp1));
    printf("   kcp2等待发送的包数量: %d\n", ikcp_waitsnd(kcp2));
    
    // 清理资源
    ikcp_release(kcp1);
    ikcp_release(kcp2);
    delete g_network;
    
    printf("\n=== 测试完成 ===\n");
    return 0;
}